package com.tenton.utils;

import java.util.Arrays;

/**
 * @ClassName: ByteUtil
 * @Description：
 * @Author： tangdong
 * @Date： 2024/6/25
 */
public class ByteUtil {

    public static byte byte2Unsigned(byte b){
        return Integer.valueOf(Byte.toUnsignedInt(b)).byteValue();
    }


    /**
     * 将int转换成高字节在前，低字节在后的byte数组。
     *
     * @param n int数字
     * @return 该int数字对应的byte数组
     */
    public static byte[] int2HBytes(int n) {
        byte[] b = new byte[4];
        b[3] = (byte) (n & 0xff);
        b[2] = (byte) (n >> 8 & 0xff);
        b[1] = (byte) (n >> 16 & 0xff);
        b[0] = (byte) (n >> 24 & 0xff);
        return b;
    }

    /**
     * 将short转换成高字节在前，低字节在后的byte数组。
     *
     * @param n short数字
     * @return 该short数字对应的byte数组
     */
    public static byte[] short2HBytes(short n) {
        byte[] b = new byte[2];
        b[1] = (byte) (n & 0xff);
        b[0] = (byte) (n >> 8 & 0xff);
        return b;
    }

    /**
     * byte 数组拼接
     *
     * @param first
     * @param rest
     * @return
     */
    public static byte[] concatBytes(byte[] first, byte[]... rest) {
        int totalLength = first.length;
        for (byte[] bytes : rest) {
            if (null != bytes) {
                totalLength += bytes.length;
            }
        }
        byte[] result = Arrays.copyOf(first, totalLength);
        int offset = first.length;
        for (byte[] bytes : rest) {
            System.arraycopy(bytes, 0, result, offset, bytes.length);
            offset += bytes.length;
        }
        return result;
    }

    /**
     * 字节码转换成 十六进制 字符串
     *
     * @param src
     * @return
     */
    public static String bytesToHexString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xff;
            String hv = Integer.toHexString(v).toUpperCase();
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }

    /**
     * 十六进制字符串转换成字节码
     *
     * @param hex
     * @return
     */
    public static byte[] hexStringToByte(String hex) {
        int len = (hex.length() / 2);
        byte[] result = new byte[len];
        char[] charArray = hex.toCharArray();
        for (int i = 0; i < len; i++) {
            int pos = i * 2;
            result[i] = (byte) (charToByte(charArray[pos]) << 4 | charToByte(charArray[pos + 1]));
        }
        return result;
    }

    public static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }

    /**
     * 将int转换成高字节在前，低字节在后的byte数组，一字节
     *
     * @param n
     * @return
     */
    public static byte[] int2HBytes1Byte(int n) {
        byte[] b = new byte[1];
        b[0] = (byte) (n & 0xff);
        return b;
    }

    /**
     * 将int转换成高字节在前，低字节在后的byte数组，二字节
     *
     * @param n
     * @return
     */
    public static byte[] int2HBytes2Byte(int n) {
        byte[] b = new byte[1];
        b[0] = (byte) (n & 0xff);
        b[1] = (byte) (n >> 8 & 0xff);
        return b;
    }

    /**
     * 将byte数组转换成二进制字符串
     *
     * @param bytes
     * @return
     */
    public static String bytesToBinaryString(byte[] bytes) {
        StringBuilder binaryBuilder = new StringBuilder();
        for (byte b : bytes) {
            int val = b;
            for (int i = 0; i < 8; i++) {
                binaryBuilder.append((val & 128) == 0 ? '0' : '1');
                val <<= 1;
            }
        }
        return binaryBuilder.toString();
    }
}
